package com.fjwt.gz.sync.ctrl.sysncdata;

import cn.hutool.json.JSONUtil;
import com.fjwt.gz.core.constants.ApiCodeEnum;
import com.fjwt.gz.core.ctrls.AbstractCtrl;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.core.exception.ResponseException;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.sync.service.PayOrderSyncService;
import com.fjwt.gz.zhyx.SignUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**同步卡券核销的交易订单**/

@RestController
@Slf4j
@RequestMapping("/api/payorder")
public class PayOrderNoticeController extends AbstractCtrl {
    @Autowired
    private PayOrderSyncService payOrderSyncService;

    @RequestMapping(value = {"/sync/{ifCode}", "/return/{ifCode}/{payOrderId}"}, method = RequestMethod.POST)
    public ApiRes syncPayOrder(HttpServletRequest request, @PathVariable("ifCode") String ifCode, @PathVariable(value = "payOrderId", required = false) String urlOrderId) {
        String payOrderId = null;
        String logPrefix = "进入小邮付[" + ifCode + "]订单回调：urlOrderId：[" + StringUtils.defaultIfEmpty(urlOrderId, "") + "] ";
        log.info("===== {} =====", logPrefix);
        try {

            // 参数有误
            if (StringUtils.isEmpty(ifCode)) {
                log.info("回调参数有误！：{}", logPrefix);
                return ApiRes.fail(ApiCodeEnum.CUSTOM_FAIL, "回调参数有误");
            }
            Map<String, String> headerByRequest = SignUtils.getHeaderByRequest(request);
            log.info("请求参数：{}", JSONUtil.toJsonStr(headerByRequest));
            String contentByRequest = SignUtils.getContentByRequest(request);
            String s = SignUtils.checkSign(headerByRequest, contentByRequest);
            log.info("验签结果：{}", s);
            if (s.length() > 0) {
                return ApiRes.fail(ApiCodeEnum.CUSTOM_FAIL, s);
            }
            Long aLong = Long.parseLong(headerByRequest.get("auth-timestamp"));
            String decryptSign = SignUtils.getDecryptSign(contentByRequest, aLong);
            log.info("解密后数据：{}", decryptSign);

            ApiRes apiRes = payOrderSyncService.syncPayOrder(decryptSign, ifCode, urlOrderId);
            return apiRes;


        } catch (BizException e) {
            log.error("{}, payOrderId={}, BizException", logPrefix, payOrderId, e);
            return ApiRes.fail(ApiCodeEnum.CUSTOM_FAIL, e.getMessage());

        } catch (ResponseException e) {
            log.error("{}, payOrderId={}, ResponseException", logPrefix, payOrderId, e);
            return ApiRes.fail(ApiCodeEnum.CUSTOM_FAIL, "系统异常");

        } catch (Exception e) {
            log.error("{}, payOrderId={}, 系统异常", logPrefix, payOrderId, e);
            return ApiRes.fail(ApiCodeEnum.CUSTOM_FAIL, "系统异常");
        }
    }
}
